package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.ContractItem;
import com.edward.model.ContractList;
import com.edward.service.ContractItemManager;
import com.edward.service.ContractListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ContractListAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private ContractItemManager contractItemManager;
	
	private ContractListManager contractListManager;
	
	private ContractList contractList;
	
	private ContractItem contractItem;
	
	private int limit;
	
	private int start;

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public ContractList getContractList() {
		return contractList;
	}

	public void setContractList(ContractList contractList) {
		this.contractList = contractList;
	}

	public ContractItem getContractItem() {
		return contractItem;
	}

	public void setContractItem(ContractItem contractItem) {
		this.contractItem = contractItem;
	}
	

	public void setContractItemManager(ContractItemManager contractItemManager) {
		this.contractItemManager = contractItemManager;
	}

	public void setContractListManager(ContractListManager contractListManager) {
		this.contractListManager = contractListManager;
	}

	//添加contractList 的表头
	@Secured("ROLE_REPORT_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		contractList.setListNum(contractListManager.listNum());
		contractListManager.addContractList(contractList);
		int total = contractListManager.listContractList().size();
		response.getWriter().print("{success : true , msg : '添加成功!',contractList_id:"+ contractList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractList b = contractListManager.getContractList(contractList.getId());
		contractList.setItems(b.getItems());
		contractList.setListNum(b.getListNum());
		contractList.setTotal(b.getTotal());
		contractListManager.updateContractList(contractList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractList s = contractListManager.getContractList(contractList.getId());
		if(s.getItems().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在Contract货物列表，请先删除Contract货物!!'}");
		}else{
			contractListManager.deleteContractList(s.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	@Secured("ROLE_REPORT")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = contractListManager.pagerList(start, limit );
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
//	//添加contractList 的 货物明细
	@Secured("ROLE_REPORT_ADD")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractList s = contractListManager.getContractList(contractList.getId());
		contractItem.setAmount(contractItem.getPrice()*contractItem.getQuantity());
		contractItemManager.addContractItem(contractItem);
		s.setTotal(s.getTotal() + contractItem.getAmount()); 
		s.getItems().add(contractItem);
		contractListManager.updateContractList(s);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractItem c =  contractItemManager.getContractItem(contractItem.getId());
		ContractList s = contractListManager.getContractList(contractList.getId());
		contractItem.setAmount(contractItem.getPrice()*contractItem.getQuantity());
		contractItem.setGross(c.getGross());
		contractItem.setPackages(c.getPackages());
		contractItem.setNet(c.getNet());
		s.setTotal(s.getTotal() + contractItem.getAmount() - c.getAmount()); 
		contractListManager.updateContractList(s);
		contractItemManager.updateContractItem(contractItem);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String deleteItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractItem c = contractItemManager.getContractItem(contractItem.getId());
		ContractList s = contractListManager.getContractList(contractList.getId());
		s.setTotal(s.getTotal() - c.getAmount() ); 
		contractListManager.updateContractList(s);
		contractItemManager.deleteContractItem(c.getId());
		response.getWriter().print("{success : true , msg : '删除成功'}");
		return null;
	}


	//获取grid 显示 的 分页信息
	@Secured("ROLE_REPORT")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(contractList.getListNum()!= null){
			pager = contractItemManager.pagerContractItem(start,limit,contractListManager.getContractList(contractList.getListNum()).getId());
		}else{
			pager = contractItemManager.pagerContractItem(start,limit,contractList.getId());
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_REPORT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	


	
	
	
	
	
	
	
	
	

}
